package com.com.yunzong.rainbowbridge.network;



import com.com.yunzong.rainbowbridge.BridgeConfig;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by  lkf  on 2017/1/5.
 * Retrofit 相关
 */

public class Bridge_RetrofitUtils {
    private static final Converter.Factory gsonConverterFactory = GsonConverterFactory.create();
    private static final CallAdapter.Factory rxJavaCallAdapterFactory = RxJavaCallAdapterFactory.create();
    private static final int DEFAULT_TIMEOUT = 15;

    private static BridgeObservable apiObservable;

    public static BridgeObservable getApiObservable() {
        if (apiObservable == null) {
            apiObservable = getRetrofit(BridgeConfig.SERVER_URL).create(BridgeObservable.class);
        }
        return apiObservable;
    }

    private static Retrofit getRetrofit(String url) {
        return new Retrofit.Builder()
                .client(new OkHttpClient
                        .Builder()
//                        .retryOnConnectionFailure(false)
                        .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                        .addInterceptor(new LogInterceptor())
                        .build())

                .baseUrl(url)
                .addConverterFactory(gsonConverterFactory)
                .addCallAdapterFactory(rxJavaCallAdapterFactory)
                .build();
    }






}


